<?php
/*
 * @分销商城
 * @功能说明：商品详情页
 * @更新说明：暂无更新
 * @文件名 GoodsController.class.php
 * @编码 UTF-8
 * @创建时间 2016-3-7 下午2:49:01
 * @创建人 XING
 */

namespace wap\Controller;
use Think\Controller;
class GoodsController extends ApibaseController
{   
    private $appId;
	private $appSecret;
    /*
	 * 分销商城_商品详情页
	 * @date: 2016-3-7 下午2:50:17
	 * @editor: XING
	 */
	public function detail(){
		forLegal($this->realSek);
		if(I('get.id')){
			$getDesc = M('GoodsEntity')
					 ->where(array('goods_id'=>I('get.id')))->find();
			$this->assign('getDesc',$getDesc);
			$this->display('goods/detail');
		}else{
			exit('非法请求');
		}
	}
	
    /*
	 * 分销商城_商品列表
	 * @date: 2016-3-7 下午4:18:56
	 * @editor: XING
	 */
	public function glist(){
		/*热销商品*/
		if(I('get.hotAjax')){
			$html = '';$page = I('get.page') ?: 1;
			$getGoods = M('GoodsEntity')->where(array('delete_at'=>'0','status'=>'1'))->where('kcun > 0');
			$sql = '';
			if(I('get.cid')){
				if(I('get.plv')){
					$sql .= 'cate2_id = '.I('get.cid');
				}else{
					$sql .= 'cate1_id = '.I('get.cid');
				}
			}
			$sql .= ($sql ? ' AND ' : '').' type = '.(I('get.oem') ? '2' : '1');
			if(I('get.keyword'))
				$sql .= ($sql ? ' AND ' : '').' goods_title LIKE "%'.I('get.keyword').'%"';
			if($sql)
				$getGoods = $getGoods->where($sql);
			if(I('get.hot'))
				$getGoods = $getGoods->order(array('sort'=>I('get.hot') == 'dw' ? 'desc' : 'asc','add_at'=>'desc'));
			if(I('get.sales'))
				$getGoods = $getGoods->order(array('sales'=>I('get.sales') == 'dw' ? 'desc' : 'asc','add_at'=>'desc'));
			if(I('get.news'))
				$getGoods = $getGoods->order(array('add_at'=>I('get.news') == 'dw' ? 'desc' : 'asc'));
			if(I('get.price')){
				$getGoods = $getGoods->order(array('goods_price'=>I('get.price') == 'dw' ? 'desc' : 'asc','add_at'=>'desc'));
			}
           
			$getGoods = $getGoods->page($page,10)->select();
			if($getGoods){
				foreach($getGoods AS $key=>$val){
					
					if(I('get.oem')){
						$html .= '<dl><a href="/goods/detail/id/'.$val["goods_id"].'/sek/'.I('get.sek').'"><dt><img class="lazys" src="/statics/images/lazy.png" data-original="'.transUrl($val['goods_pic']).'" width="100%"></dt><dd class="ginza-title">'.$val["goods_title"].'</dd></a><dd class="ginza-pay" >￥'.$val["goods_price"].'</dd><dd class="ginza-buy" ><p onclick="putCart(this)" class="icon-gouwu4"><span>加入购物车</span></p></dd><input type="hidden" value="'.$val["goods_id"].'" class="goodsId" /></dl>';
					}else{
					    if(!($key%2)){
						    $html .= '</ul><ul>';
					    }
					    $html .= '<li style="position:relative"><a href="/goods/detail/id/'.$val["goods_id"].'/sek/'.I('get.sek').'"><img class="lazys" src="/statics/images/lazy.png" data-original="'.transUrl($val['goods_pic']).'" width="100%" >'.($val['kcun']?'':'<span class="soldout"></span>').'</a><p><a href="/goods/detail/id/'.$val["goods_id"].'">'.$val["goods_title"].'</a></p><p class="pay"><span class="icon-gouwu2" onclick="putCart(this)"></span>¥'.$val["goods_price"].'</p><input type="hidden" value="'.$val["goods_id"].'" class="goodsId" /></li>';
					    if(!isset($getGoods[$key+1])){
						    $html .= '</ul>';
					    }	
					}
					
			
				}
		
				echo json_encode(array('code'=>'1','msg'=>'success','data'=>$html,'page'=>$page));exit;
			}else{
				echo json_encode(array('code'=>'0','msg'=>'success','data'=>'','page'=>$page));exit;
			}
		}
		
		forLegal($this->realSek);
		if(I('get.oem')){
			$this->appId = 'wx1aa38ea2d042537d';
		    $this->appSecret = '98da9e129edc24f8ef4b85906f7b9a8e';
			$this->inlevel = M('CustEntity')->where(array('cust_id'=>session('loginUid')))->getField('salelevel');
		    $signPackage = $this->getSignPackage();
		    $this->assign('signPackage',$signPackage);
			$this->display('goods/goem');
		}else{
			$this->display('goods/glist');
		}
	}
	
	public function getSignPackage(){
		$jsapiTicket = $this->getJsApiTicket();
		$url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
		$timestamp = time();
		$nonceStr = $this->createNonceStr();
		// 这里参数的顺序要按照 key 值 ASCII 码升序排序
		$string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr&timestamp=$timestamp&url=$url";
		$signature = sha1($string);
		$signPackage = array(
		  "appId"     => $this->appId.trim(),
		  "nonceStr"  => $nonceStr,
		  "timestamp" => $timestamp,
		  "url"       => $url,
		  "signature" => $signature,
		  "rawString" => $string
		);
		return $signPackage; 
	}
	
	private function createNonceStr($length = 16) {
		$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
		$str = "";
		for ($i = 0; $i < $length; $i++) {
			$str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
		}
		return $str;
	}
	
	private function getJsApiTicket() {
		$data=$this->getAccessToken();
		$url ='https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token='.$data.'&type=jsapi';
		$res = json_decode($this->httpGet($url),true);
		$ticket = $res['ticket'];
		return $ticket;
    }

    public function getAccessToken() {
		$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$this->appId."&secret=".$this->appSecret;
		$res = json_decode($this->httpGet($url),true);
		$data=$res['access_token'];
	    return $data;
    }

	function httpGet($url)
	{
		$curl = curl_init();
		curl_setopt($curl, CURLOPT_URL, $url);
		curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
		curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
		$output = curl_exec($curl);
		curl_close($curl);
		return $output;
	}
	
	
	/*
	 * 分销商城_商品收藏
	 * @date: 2016-5-9 下午4:09:56
	 * @editor: BIELANG
	 */
	
	public function collect(){
	    if(I('get.shoucang')){
			$collect = M('GoodsCollect')->where(array('goods_id'=>I('get.goods_id'),'cust_id'=>session('loginUid')))->find();
			if($collect){
				$delcol = M('GoodsCollect')->where(array('goods_id'=>I('get.goods_id'),'cust_id'=>session('loginUid')))->delete();
				if($delcol){
			        echo json_encode(0);exit;   
				}
			}
			$data = M('GoodsCollect')->add(array(
			            'goods_id'=>trim(I('get.goods_id')),
						'cust_id'=>session('loginUid'),
						'add_at'=>time()
			        ));
			if($data){
				echo json_encode(1);exit; 
			}else{
				echo json_encode(2);exit; 
			}
		}	
			
		
	}
	
	/*
	 * 分销商城_我的收藏页面
	 * @date: 2016-5-9 下午5:09:56
	 * @editor: BIELANG
	 */
	
    public function myCollect(){
	    $goodscol = M('GoodsCollect')->where(array('cust_id'=>session('loginUid')))->select();
		$data = array();
	    foreach($goodscol as $val){
			$data[] = M('GoodsEntity')->where(array('goods_id'=>$val['goods_id']))->field('goods_pic,goods_id,goods_title,goods_price')->find();
		}
	    $this->assign('data',$data);
	    $this->display('goods/mycollect');
	
	}
	
	/*
	 * 分销商城_收藏删除
	 * @date: 2016-5-9 下午5:30:56
	 * @editor: BIELANG
	 */
	public function coldel(){
		if(I('get.del')){
		    $del = M('GoodsCollect')->where(array('goods_id'=>I('get.goods_id'),'cust_id'=>session('loginUid')))->delete();
		    if($del){
				echo json_encode(1);exit; 
			}else{
				echo json_encode(0);exit; 
			}
		
	    }
	
	}
	
	
}